package com.hzhr.iot.collect.protocol.util;

import com.hzhr.iot.collect.util.StrUtil;
import com.hzhr.iot.core.util.HzhrStringUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class NbUtil {
    public static String convertAlarmCodes(String alarmType) {
        try {
            String bin = HzhrStringUtils.fillZeroForLeft(StrUtil.decimal2binary(Integer.parseInt(StrUtil.hexStr2decStr(alarmType))), 16);
            if (bin.equals("0000000000000000")) return null;
            List<Integer> alarmTypes = new ArrayList<>();
            for (int i = 0; i < 16; i++) {
                if (bin.charAt(i) == '1') {
                    alarmTypes.add(i);
                }
            }

            String reuslt = "";
            for (Integer alarmTypeItem :
            alarmTypes) {
                alarmTypeItem = 16 - alarmTypeItem;
                if (alarmTypeItem == 1) {
                    reuslt += "1031,";
                } else if (alarmTypeItem == 2) {
                    reuslt += "1032,";
                } else if (alarmTypeItem == 3) {
                    reuslt += "1033,";
                } else if (alarmTypeItem == 4) {
                    reuslt += "1034,";
                } else if (alarmTypeItem == 5) {
                    reuslt += "1035,";
                } else if (alarmTypeItem == 6) {
                    reuslt += "1036,";
                } else if (alarmTypeItem == 7) {
                    reuslt += "1037,";
                } else if (alarmTypeItem == 8) {
                    reuslt += "1038,";
                }
            }

            if (!StringUtils.isEmpty(reuslt)) {
                return reuslt.substring(0, reuslt.length() - 1);
            }

            return null;
        } catch (Throwable e) {
            return null;
        }
    }
}
